<template>
	<view class="collection">
		<view class="not-img" v-if="listData.length <= 0">
			<image class="not" src="../../static/notcollection.png" mode=""></image>
			<view class="not-txt">
				暂无浏览记录
			</view>
		</view>


		<view class="main" v-else>

			<uni-swipe-action>
				<uni-swipe-action-item @click="bindClick" v-for="(item,index) in listData">

					<view class="content-box" @click="contentClick(item.id)">
						<view class="content-text">
							<!-- 插12312312313 -->
							<view class="content-item">

								<view class="left-img">
									<image class="avatar" :src="item.image" mode=""></image>
								</view>
								<view class="right-txt">
									<view class="right-txt-title">
										{{item.title}}
									</view>
									<view class="right-txt-date">
										{{item.date}}
									</view>
								</view>
							</view>
						</view>
					</view>
					<template v-slot:right>
						<view class="slot-button" @click="bindClick(index)"><text class="slot-button-text">取消收藏</text>
						</view>
					</template>
				</uni-swipe-action-item>
			</uni-swipe-action>

		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				listData: [],
				time: null,
				index: 0,
				index2: 0
			}
		},
		onLoad() {
			this.loading()
			
		},
		methods: {
			loading() {
				let arr;
				try {
					const value = uni.getStorageSync('collection');
					if (value) {
						arr = value
					}
				} catch (e) {
					// error
				}
				if (arr.length != 0) {
					uni.request({
						url: `https://apis.netstart.cn/zhihudaily/story/${arr[this.index].contentid}`,
						success: res => {
							// console.log(arr);
							this.listData.push({
								image: res.data.story.image,
								title: res.data.story.title,
								id: res.data.story.id,
								date: arr[this.index].time
							});
							//迭代解决for循环单线程未能异步请求到数据导致的顺序错乱问题
							if (this.index < arr.length - 1) {
								this.index++
								this.loading()
							} else {
								return
							}
						}
					})
				} else {
					// return
					console.log("没了")
				}

			},
			bindClick(index) {
				// console.log(index)
				let newArr;
				let arr = uni.getStorage({
					key: 'collection',
					success: function(res) {
						return newArr = res.data
					}
				});
				newArr.splice(index, 1)
				let that = this
				uni.setStorage({
					key: 'collection',
					data: newArr,
					success: function() {
						// location.reload();
						// console.log("下标",that.index)
						that.index = 0
						that.listData = []
						that.loading()
					}
				});
				// getdata(() =>{

				// })
				// if (newArr.length >= 0) {
				// 	this.listData = []
				// 	uni.request({
				// 		url: `https://apis.netstart.cn/zhihudaily/story/${newArr[this.index2].contentid}`,
				// 		success: res => {
				// 			// console.log(arr);
				// 			this.listData.push({
				// 				image: res.data.story.image,
				// 				title: res.data.story.title,
				// 				id: res.data.story.id,
				// 				date: newArr[this.index2].time
				// 			});
				// 			//递归解决for循环单线程未能异步请求到数据导致的顺序错乱问题
				// 			if (this.index2 < newArr.length - 1) {
				// 				this.index++
				// 				this.loading()
				// 			} else {
				// 				return
				// 			}
				// 		}
				// 	})
				// }

			},

		}
	}
</script>
<style scoped lang="less">
	.not-img {
		margin: 400rpx auto;
		text-align: center;

		.not {
			width: 148px;
			height: 110px;
		}

		.not-txt {
			margin-top: 20rpx;
			color: #ccc;
		}
	}

	.order-item {
		width: 100%;
		// display: flex;
		position: relative;
		margin: 10px auto;
		align-items: right;
		// flex-direction: row;
	}

	.title {
		padding-left: 20rpx;
	}

	/deep/.uni-list-chat__content-note[data-v-3d9bc00b] {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		width: 290rpx;
		display: block;
	}

	/deep/.header--circle[data-v-3d9bc00b] {
		border-radius: 0;
	}

	/deep/.uni-list-chat__content-extra[data-v-3d9bc00b] {
		display: inline;
	}

	.content-box {
		flex: 1;
		/* #ifdef APP-NVUE */
		justify-content: center;
		/* #endif */
		height: 44px;
		line-height: 44px;
		padding: 0 15px;
		position: relative;
		background-color: #fff;
		border-bottom-color: #f5f5f5;
		border-bottom-width: 1px;
		border-bottom-style: solid;
	}

	.content-item {
		display: flex;
	}

	.example-body {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: center;
		padding: 10px 0;
		background-color: #fff;
	}

	.button {
		border-color: #e5e5e5;
		border-style: solid;
		border-width: 1px;
		padding: 4px 8px;
		border-radius: 4px;
	}

	.button-text {
		font-size: 15px;
	}

	.slot-button {
		/* #ifndef APP-NVUE */
		display: flex;
		height: 100%;
		/* #endif */
		flex: 1;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding: 0 20px;
		background-color: #ff5a5f;
	}

	.slot-button-text {
		color: #ffffff;
		font-size: 14px;
	}

	.content-box[data-v-74d0bef4] {
		height: 100%;
		line-height: 100%;
		margin-bottom: 20rpx;

	}

	.avatar {
		width: 200rpx;
		height: 200rpx;
	}

	.right-txt {
		padding: 20rpx 0 0 20rpx;
	}

	.right-txt-title {
		text-overflow: -o-ellipsis-lastline;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		line-clamp: 2;
		-webkit-box-orient: vertical;
	}

	.right-txt-date {
		margin-top: 30rpx;
		color: #C0C4CC;
	}
</style>
